package com.example.good.service.impl;

import com.example.good.entity.Address;
import com.example.good.mapper.AddressMapper;
import com.example.good.service.IAddressService;
import com.example.good.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Value("${user.address.max-count}")
    private Integer maxCount;
    @Override
    public Integer addNewAddress(Integer uid, String username, Address address) {
        Integer count = addressMapper.countByUid(uid);
        if (count >= maxCount){
            throw new AddressCountLimitException("地址数量上限");
        }
        Integer isDefault = count == 0 ? 1 : 0;
        address.setProvinceName(address.getProvinceName());
        address.setCityName(address.getCityName());
        address.setAreaName(address.getAreaName());
        address.setUid(uid);
        address.setIsDefault(isDefault);
        address.setCreatedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());
        Integer rows = addressMapper.insert(address);
        if (rows != 1){
            throw new InsertException("插入异常");
        }
        return address.getAid();
    }

    @Override
    public List<Address> getByUid(Integer uid) {
        List<Address> list = addressMapper.findByUid(uid);
        for (Address address : list){
            address.setUid(null);
            address.setCreatedUser(null);
            address.setCreatedTime(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        return list;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result == null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
        Integer rows = addressMapper.updateNoDefault(uid);
        if (rows < 1 ){
            throw new UpdateException("更新异常");
        }
        rows = addressMapper.updateDefaultByAid(aid,username,new Date());
        if (rows != 1){
            throw new UpdateException("更新异常");
        }
    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result == null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
        Integer rows = addressMapper.deleteByAid(aid);
        if (rows != 1){
            throw new DeleteException("删除异常");
        }
        Integer count = addressMapper.countByUid(uid);
        if (count == 0 ){
            return; //直接终止程序
        }
        if (result.getIsDefault() == 0){
            return; //如果删掉的不是默认地址，终止程序，不往下进行
        }
        Address address= addressMapper.findLastModified(uid);
        rows = addressMapper.updateDefaultByAid(
                address.getAid(),username,new Date()
        );
        if (rows != 1){
            throw new UpdateException("更新异常");
        }
    }

    @Override
    public Address getByAid(Integer aid, Integer uid) {
        Address address = addressMapper.findByAid(aid);
        if (address == null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if (!address.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
        address.setCreatedTime(null);
        address.setCreatedUser(null);
        address.setModifiedUser(null);
        address.setModifiedTime(null);
        return address;
    }

    @Override
    public void updateAddressByAid(Integer aid,Integer uid, String username, Address address) {
        Address result = addressMapper.findByAid(aid);
        if (result == null) {
            throw new AddressNotFoundException("地址不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
        address.setAid(aid);
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());
        Integer rows = addressMapper.updateAddressByAid(address);
        if (rows != 1){
            throw new UpdateException("更新错误");
        }
    }
}
